package com.panda.controller.article;

import com.panda.constants.SystemConstants;
import com.panda.entity.vo.*;
import com.panda.service.CommentService;
import com.panda.service.ArticleService;
import com.panda.utils.RespondResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.util.List;


@Api(tags = "文章相关接口")
@RestController
@Validated
@RequestMapping("/articles")
public class ArticleController {

    private final ArticleService articleService;
    private final CommentService commentService;

    @Autowired
    public ArticleController(ArticleService articleService, CommentService commentService) {
        this.articleService = articleService;
        this.commentService = commentService;
    }

    @ApiOperation("获取所有文章")
    @GetMapping
    public RespondResult<PageVo<ArticleListVo>> getAllArticles(@NotNull(message = "页码不能为空") Integer pageNum, @NotNull(message = "每页大小不能为空") Integer pageSize, Long categoryId, String keyword) {
        return articleService.getAllArticles(null, pageNum, pageSize, categoryId, keyword);
    }

    @ApiOperation("上传一篇文章")
    @PostMapping
    public RespondResult<ArticleVo> postArticle(@Validated @RequestBody ArticleVo articleVo) {
        return articleService.postArticle(articleVo);
    }

    @ApiOperation("查看热门文章")
    @GetMapping("/top")
    public RespondResult<List<TopArticleVo>> getTopArticles() {
        return articleService.getTopArticles();
    }

    @ApiOperation("文章详情")
    @GetMapping("/{id}")
    public RespondResult<ArticleDetailVo> getArticleDetail(@PathVariable("id") Long id) {
        return articleService.getArticleDetail(id);
    }

    @ApiOperation("删除文章")
    @DeleteMapping("/{id}")
    public RespondResult deleteArticle(@PathVariable("id") Long id) {
        return articleService.deleteArticle(id);
    }

    @ApiOperation("修改文章")
    @PutMapping("/{id}")
    public RespondResult<ArticleVo> modifyArticle(@PathVariable("id") Long id, @Validated @RequestBody ArticleVo articleVo) {
        return articleService.modifyArticle(id, articleVo);
    }

    @ApiOperation("文章评论")
    @GetMapping("/{id}/comments")
    public RespondResult<PageVo<CommentVo>> getComment(@PathVariable("id") Long id, @NotNull(message = "页码不能为空") Integer pageNum, @NotNull(message = "每页大小不能为空") Integer pageSize) {
        return commentService.getComment(SystemConstants.ARTICLE_COMMENT_TYPE, id, pageNum, pageSize);
    }

    @ApiOperation("发表评论")
    @PostMapping("/{id}/comments")
    public RespondResult<CommentVo> postComment(@PathVariable("id") Long id, @Validated @RequestBody CommentVo commentVo) {
        return commentService.postComment(SystemConstants.ARTICLE_COMMENT_TYPE, id, commentVo);
    }

    @ApiOperation("搜索文章")
    @GetMapping("search")
    public RespondResult getSearchArticle(@RequestParam String keyword, @RequestParam String pageNum) {
        return articleService.searchArticle(keyword, Long.parseLong(pageNum));
    }
}
